import JsBridge from '@/utils/JsBridge.js'
import { createRouter, createWebHashHistory } from "vue-router";
import { getStore, setStore, globalLoading } from "../utils/utils";
import store from '@/store'

const routes = [
  {
    path: "/",
    component: () => import("@/views/login"),
  },
  {
    path: "/forget",
    component: () => import("@/views/login/forget"),
  },
  {
    path: "/register",
    component: () => import("@/views/login/register"),
  },
  {
    path: "/realName",
    component: () => import("@/views/login/realName"),
  },
  {
    path: "/share",
    name: "分享",
    component: () => import("@/views/share"),
    meta: {
      openName: "/share",
    },
  },
  {
    path: "/share/register",
    name: "分享注册",
    component: () => import("@/views/share/register"),
  },
  {
    path: "/home",
    component: () => import("@/views/home"),
    redirect: "/index",
    children: [
      {
        path: "/tongji",
        name: "统计",
        component: () => import("@/views/tongji"),
        meta: {
          openName: "/tongji",
        },
      },
      // 商品
      {
        path: "/index",
        name: "商品",
        component: () => import("@/views/index"),
        meta: {
          openName: "/index",
        },
      },
      {
        path: "/goods/detail",
        name: "商品详情",
        component: () => import("@/views/index/goods/detail"),
        meta: {
          openName: "/goods",
        },
      },
      {
        path: "/goods/info",
        name: "商品资料",
        component: () => import("@/views/index/goods/info"),
        meta: {
          openName: "/goods",
        },
      },
      {
        path: "/goods/order",
        name: "提交订单",
        component: () => import("@/views/index/goods/order"),
        meta: {
          openName: "/goods",
        },
      },
      {
        path: "/goods/pay",
        name: "去支付",
        component: () => import("@/views/index/goods/pay"),
        meta: {
          openName: "/goods",
        },
      },

      // 订单
      {
        path: "/order",
        name: "订单",
        component: () => import("@/views/order"),
        meta: {
          openName: "/order",
        },
      },
      {
        path: "/order/detail",
        name: "订单详情",
        component: () => import("@/views/order/detail"),
        meta: {
          openName: "/order/detail",
        },
      },
      {
        path: "/order/success",
        name: "订单支付成功",
        component: () => import("@/views/order/success"),
        meta: {
          openName: "/order/success",
        },
      },

      // 收益
      {
        path: "/capital",
        name: "收益",
        component: () => import("@/views/capital"),
        meta: {
          openName: "/capital",
        },
      },
      {
        path: "/capital/withdraw",
        name: "提现",
        component: () => import("@/views/capital/withdraw"),
        meta: {
          openName: "/withdraw",
        },
      },
      {
        path: "/capital/withdraw/list",
        name: "提现记录",
        component: () => import("@/views/capital/withdraw/list"),
        meta: {
          openName: "/withdraw",
        },
      },
      {
        path: "/capital/withdraw/detail",
        name: "提现记录详情",
        component: () => import("@/views/capital/withdraw/detail"),
        meta: {
          openName: "/withdraw",
        },
      },
      {
        path: "/capital/account",
        name: "提现账号管理",
        component: () => import("@/views/capital/account"),
        meta: {
          openName: "/account",
        },
      },
      {
        path: "/capital/account/zfb",
        name: "绑定支付宝",
        component: () => import("@/views/capital/account/zfb"),
        meta: {
          openName: "/account",
        },
      },
      {
        path: "/capital/account/yhk",
        name: "绑定银行卡",
        component: () => import("@/views/capital/account/yhk"),
        meta: {
          openName: "/account",
        },
      },
      {
        path: "/capital/commission/list",
        name: "佣金订单",
        component: () => import("@/views/capital/commission/list"),
        meta: {
          openName: "/commission",
        },
      },

      // 我的
      {
        path: "/more",
        name: "我的",
        component: () => import("@/views/more"),
        meta: {
          openName: "/more",
        },
      },
      {
        path: "/more/shop",
        name: "店铺信息",
        component: () => import("@/views/more/shop"),
        meta: {
          openName: "/shop",
        },
      },
      {
        path: "/more/shop/update",
        name: "店铺信息修改",
        component: () => import("@/views/more/shop/update"),
        meta: {
          openName: "/shop",
        },
      },
      {
        path: "/more/shop/cardQr",
        name: "店铺二维码",
        component: () => import("@/views/more/shop/cardQr"),
        meta: {
          openName: "/shop",
        },
      },
      {
        path: "/more/agent",
        name: "我的代理商",
        component: () => import("@/views/more/agent"),
        meta: {
          openName: "/agent",
        },
      },
      {
        path: "/agentShop",
        name: "我的代理商New",
        component: () => import("@/views/more/agentShop"),
        meta: {
          openName: "/agentShop",
        },
      },
      {
        path: "/daizhuc",
        name: "代注册",
        component: () => import("@/views/login/daizhuc"),
        meta: {
          openName: "/daizhuc",
        },
      },
      {
        path: "/shopYongConfig",
        name: "代理商佣金设置",
        component: () => import("@/views/more/agentShop/shopYongConfig"),
        meta: {
          openName: "/shopYongConfig",
        },
      },
      {
        path: "/yongConfig",
        name: "分佣设置",
        component: () => import("@/views/yongConfig"),
        meta: {
          openName: "/yongConfig",
        },
      },
      {
        path: "/more/changePwd",
        name: "修改登录密码",
        component: () => import("@/views/more/changePwd"),
        meta: {
          openName: "/changePwd",
        },
      },
      {
        path: "/more/changePhone",
        name: "更换手机号码",
        component: () => import("@/views/more/changePhone"),
        meta: {
          openName: "/changePhone",
        },
      },
      {
        path: "/more/wechat",
        name: "系统客服",
        component: () => import("@/views/more/wechat"),
        meta: {
          openName: "/wechat",
        },
      },

      // 消息中心
      {
        path: "/msg",
        name: "消息中心",
        component: () => import("@/views/msg"),
        meta: {
          openName: "/msg",
        },
      },
      {
        path: "/msg/detail",
        name: "消息中心-详情",
        component: () => import("@/views/msg/detail"),
        meta: {
          openName: "/msg",
        },
      },
      {
        path: "/msg/richText",
        name: "消息中心-富文本",
        component: () => import("@/views/msg/richText"),
        meta: {
          openName: "/msg",
        },
      },
    ]
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

const wtiterPath = ["/forget", "/register", "/", "/realName"];
let login = false

export const getLoginUser = () => {
  login = true
  const isApp = store.state.isApp
  return new Promise((resolve, reject) => {
    if (isApp) {
      JsBridge.callHandler('getLoginUser', {}, (response) => {
        let res = JSON.parse(response)
        if (res.userInfo) {
          let data = JSON.parse(res.userInfo)
          setStore('USER_INFO_MF', data)
          resolve(true)
        } else {
          resolve(false)
        }
      })
    } else {
      const loginUser = getStore("USER_INFO_MF") || {}
      const { token } = loginUser
      resolve(!!token)
    }
  })
}

export const setStatusBarTextColor = (color) => {
  JsBridge.callHandler('setStatusBarTextColor', color, (res) => {
    console.log('setStatusBarTextColor 响应值 ： ', res);
  })
}

router.beforeEach((to, from, next) => {
  let userInfo = getStore('USER_INFO_MF')

  // 分享不走基座
  if (to.path == "/share" || to.path == "/share/register") {
    next()
    return
  }

  // 设置头的字体颜色
  if (to.path == '/') {
    setStatusBarTextColor({ color: 'dark' })
  } else {
    setStatusBarTextColor({ color: 'light' })
  }

  //路由导航守卫
  globalLoading(true);

  if (JsBridge?.isDev()) {
    next()
    return
  }

  // 第一次加载app，判断是否免登录
  if (!login) {
    getLoginUser().then(res => {
      if (res) {
        next({ path: "/home" });
      } else {
        next();
      }
    })
  } else {
    if (!userInfo && !wtiterPath.includes(to.path)) {
      next({ path: "/" });
      return;
    }
    next();
  }
});

router.afterEach((to, from) => {
  globalLoading(false);
});

export default router;